package com.ruoyi.api.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DzKaoshi;
import com.ruoyi.system.service.IDzKaoshiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考试信息Controller
 * 
 * @author ruoyi
 * @date 2022-09-02
 */
@RestController
@RequestMapping("/api/kaoshi")
public class DzKaoshiApiController extends BaseController
{
    @Autowired
    private IDzKaoshiService dzKaoshiService;

    @Autowired
    private ISysDeptService sysDeptService;


    /**
     * 查询考试信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DzKaoshi dzKaoshi)
    {
        startPage();
if(dzKaoshi.getDeptId()==null){
    dzKaoshi.setDeptId(999l);
}else{
    SysDept dept =  sysDeptService.selectDeptById(dzKaoshi.getDeptId());
    if(dept!=null&&dept.getAncestors()!=null){
        String[] arr = dept.getAncestors().split(",");
        if(arr!=null&&arr.length>2){
            String newdeptid = arr[2];
            dzKaoshi.setDeptId(Long.parseLong(newdeptid));
        }
    }
}

        List<DzKaoshi> list = dzKaoshiService.selectDzKaoshiListWeb(dzKaoshi);
        return getDataTable(list);
    }

    /**
     * 导出考试信息列表
     */
    @Log(title = "考试信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DzKaoshi dzKaoshi)
    {
        List<DzKaoshi> list = dzKaoshiService.selectDzKaoshiListWeb(dzKaoshi);
        ExcelUtil<DzKaoshi> util = new ExcelUtil<DzKaoshi>(DzKaoshi.class);
        util.exportExcel(response, list, "考试信息数据");
    }

    /**
     * 获取考试信息详细信息
     */
    @GetMapping(value = "/{ksId}")
    public AjaxResult getInfo(@PathVariable("ksId") Long ksId)
    {
        return AjaxResult.success(dzKaoshiService.selectDzKaoshiByKsId(ksId));
    }

    /**
     * 新增考试信息
     */
    @Log(title = "考试信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DzKaoshi dzKaoshi)
    {
        return toAjax(dzKaoshiService.insertDzKaoshi(dzKaoshi));
    }

    /**
     * 修改考试信息
     */
    @Log(title = "考试信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DzKaoshi dzKaoshi)
    {
        return toAjax(dzKaoshiService.updateDzKaoshi(dzKaoshi));
    }

    /**
     * 删除考试信息
     */
    @Log(title = "考试信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ksIds}")
    public AjaxResult remove(@PathVariable Long[] ksIds)
    {
        return toAjax(dzKaoshiService.deleteDzKaoshiByKsIds(ksIds));
    }
}
